package com.innovation.chengzhangriji.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;

import java.lang.ref.SoftReference;

public class Html5Activity extends BaseActivity implements CuteToolbar.OnClickCuteToolbarListener {

    private CuteToolbar toolbar;
//    private XWalkView webView;
    private RelativeLayout contentView;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html5);
    }

    // 初始化视图
    private void setupLayout() {

        // 导航栏
        toolbar = (CuteToolbar) findViewById(R.id.toolbar);
        toolbar.setOnClickCuteToolbarListener(this);
        // toolbar.setCuteTitle(getIntent().getStringExtra("title"));

        // webView
//        webView = (XWalkView) findViewById(R.id.webView);
//        webView.clearCache(true);
//        webView.load(getIntent().getStringExtra("url"), null);

        contentView = (RelativeLayout) findViewById(R.id.content);
        // 为兼容4.4之前的webView这种方式可防止内存泄漏
        webView = new WebView(getApplicationContext());
        webView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        webView.setBackgroundColor(getResources().getColor(R.color.theme_bg));
        if(Client.isNetworkConnect(this)) {
            webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        }
        webView.loadUrl(getIntent().getStringExtra("url") + "?uid=" + Config.getLoginInfo(this).getUserID());
        ((RelativeLayout)contentView).addView(webView, 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 初始化视图
        setupLayout();
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

    }

    @Override
    protected void onStop() {
        if(webView != null) {
            webView.removeAllViews();
            ((RelativeLayout)contentView).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onStop();
    }
}
